package com.dataseek.iot.product.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dataseek.iot.product.entity.PdtAttributeDic;
import com.dataseek.iot.product.service.AttrService;
import com.dataseek.iot.product.vo.AttrVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/attribute")
public class AttrController {

    @Autowired
    private AttrService attrService;


    /**
     * 查询产品属性列表
     *
     * @param attrName 产品属性名称
     * @return
     */

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseResult queryAttr(@RequestParam(value="page")  Integer page, @RequestParam(value="size")  Integer size, @RequestParam(value="attrName", required=false) String attrName ) throws IOException {
        AttrVo attrVo = attrService.queryAttr(attrName,page,size);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),attrVo);
    }

    /**
     * 修改产品属性列表
     *
     * @param pdtAttributeDic 产品属性实体类
     * @return
     */

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult updateAttr(@RequestBody PdtAttributeDic pdtAttributeDic) throws IOException {
        Integer code  = attrService.updateAttr(pdtAttributeDic);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }

    /**
     * 新建产品属性
     *
     * @param pdtAttributeDic 产品属性实体类
     * @return
     */

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult insertAttr(@RequestBody PdtAttributeDic pdtAttributeDic) throws IOException, SQLException {
        Integer code  = attrService.insertAttr(pdtAttributeDic);
        ResponseResult responseResult = null;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult = new ResponseResult(RspStatusEnum.FAIL.getCode(),"产品属性编码字段重复，请重新输入!",code.toString());
        } else {
            responseResult = new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
        }
        return responseResult;
    }


    /**
     * 删除产品属性
     *
     * @param ids 产品属性ID,满足批量删除
     * @return
     */

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseResult deleteAttr(@RequestBody JSONObject ids)  throws IOException {
        JSONArray jsonArray = ids.getJSONArray("ids");
        List<Long> idList = JSONObject.parseArray(jsonArray.toJSONString(), Long.class);
        Integer code  = attrService.deleteAttr(idList);
        ResponseResult responseResult = null;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult = new ResponseResult(RspStatusEnum.FAIL.getCode(),"该属性在使用中，无法删除!",code.toString());
        } else {
            responseResult = new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
        }
        return responseResult;
    }



}
